﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Tesla.Service.DataContracts;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using System.ServiceModel;
using Tesla.Service.WcfServiceCommon;
//using Telerik.Web.UI;
using Tesla.Util.Utilities;
using System.IO;

namespace Tesla.Web.Pages
{
    public partial class ProjectDetails : PageBase
    {
        #region Class Level Variables
        private ProjectServiceDispatcher projectDispatcher = null;
        private int? ProjectRecordCount = 0;
        #endregion


        protected void Page_Load(object sender, EventArgs e)
        {
            List<ProjectDC> project = null;

            projectDispatcher = new ProjectServiceDispatcher();

            try
            {
                if (!IsPostBack)
                {
                    if (Request.QueryString["ID"] != null)
                    {
                        project = projectDispatcher.ReadProject(Convert.ToInt32(Request.QueryString["ID"])).Cast<ProjectDC>().ToList();
                        SetValuesForControls(project);
                        Legend1.InnerText = "Update Project";
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                int? projectID = 0, success = 0;
                ProjectDC createdProject = new ProjectDC();
                createdProject.ProjectCode = txtProjectCode.Text;
                createdProject.ProjectBillingType = Convert.ToInt32(txtProjectBillingType.Text);
                createdProject.ProjectType = Convert.ToInt32(txtProjectType.Text);
                createdProject.ProjectPlannedStartDate = Convert.ToDateTime(txtProjectPlannedStartDate.Text);
                createdProject.ProjectPlannedEndDate = Convert.ToDateTime(txtProjectPlannedEndDate.Text);
                projectDispatcher = new ProjectServiceDispatcher();

                if (ViewState["SelectedProjectId"] == null)
                {
                    projectID = projectDispatcher.CreateProject(createdProject);
                }
                else
                {
                    createdProject.ProjectID = Convert.ToInt32(ViewState["SelectedProjectId"]);
                    success = projectDispatcher.UpdateProject(createdProject);
                }
                if (!Page.ClientScript.IsClientScriptBlockRegistered("saveAlert"))
                {
                    Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "saveAlert", "alert('Data has been succesfully saved '); window.location.href('ProjectSearch.aspx')", true);
                }


            }

            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void SetValuesForControls(List<ProjectDC> project)
        {
            ViewState["SelectedProjectId"] = ((ProjectDC)project[0]).ProjectID;
            this.txtProjectCode.Text = ((ProjectDC)project[0]).ProjectCode;
            this.txtProjectBillingType.Text = ((ProjectDC)project[0]).ProjectBillingType.ToString();
            this.txtProjectType.Text = ((ProjectDC)project[0]).ProjectType.ToString();
            this.txtProjectPlannedStartDate.Text = ((ProjectDC)project[0]).ProjectPlannedStartDate.ToString();
            this.txtProjectPlannedEndDate.Text = ((ProjectDC)project[0]).ProjectPlannedEndDate.ToString();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect(VirtualPathUtility.ToAbsolute("~/") + @"Pages/Administrator/ProjectSearch.aspx", false);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
